﻿(function (ns) {
    ZYCLib.RegNameSpace(ns).Sign = (function () {
        var isPass = true,
            procceing = false;

        var ShowErrorMessage = function (message) {
            ZYCLib.UI.MessageBox.Alert(message);
            isPass = false;
        };

        var HideErrorMessage = function () {
            isPass = true;
        };

        var CheckInput = function (id, message, regExp, ismacth) {
            var isValidated = ZYCLib.Text.InputValidator(id, regExp, ismacth);
            if (!isValidated) {
                ShowErrorMessage(message);
                $("#" + id).focus();
            }
            return isValidated;
        };

        var FindPassword = function () {
            HideErrorMessage();
            if (!CheckInput("txtMobile", "请输入手机号")) return;
            if (!CheckInput("txtMobile", "手机号格式不正确，请重新输入", "^1\\d{10}$")) return;
            if (!CheckInput("txtSMSVCode", "请输入手机验证码")) return;
            var usermobileIpt = $("#txtMobile");
            var checkcodeIpt = $("#txtSMSVCode");
            $("#nextStep").text("处理中…");

            if (procceing === true) {
                ShowErrorMessage("正在处理中，请稍后...");
                return;
            }

            procceing = true;
            if (!isPass) return;
            var data = { Mobile: usermobileIpt.val(), SMSVCode: checkcodeIpt.val() };

            ZYCLib.Web.JsonAjax.NPostData("UserSecureControlService/FindPasswordVerify", data, function (jsondata) {
                if (jsondata != null) {
                    if (jsondata.ErrorCode === 0) {
                        $("#nextStep").text("设置新密码");
                        var inputItems = [];
                        var inputItem = { Id: "txtMobile", Name: "txtMobile", Type: "hidden", Value: usermobileIpt.val() };
                        var inputItem2 = { Id: "txtSMSVCode", Name: "txtSMSVCode", Type: "hidden", Value: checkcodeIpt.val() };
                        inputItems.push(inputItem);
                        inputItems.push(inputItem2);
                        ZYCLib.Utils.Builder.BuildFormAndSubmit("post", "/WebPage/ResetPwd", "_self", inputItems);
                    } else {
                        $("#nextStep").text("下一步");
                        ShowErrorMessage(jsondata.ErrorMessage);
                    }
                }
                procceing = false;
            });
        };

        var FindPasswordInit = function () {
            var bindLoginEventItems = ['txtMobile', 'txtSMSVCode'];
            $.each(bindLoginEventItems, function (index, val) {
                $("#" + val).on("keypress", function(event) {
                    var ev = event || window.event;
                    if (ev.keyCode === 13) {
                        FindPassword();
                        return false;
                    }
                    return true;
                });
            });

            var RemainSeconds = 60;

            var SendSVCode = function (id, verificationCodeType) {
                if (!CheckInput("txtMobile", "请输入手机号")) return;
                if (!CheckInput("txtMobile", "手机号格式不正确，请重新输入", "^1\\d{10}$")) return;
                var text = $("#" + id).html();
                var usermobileIpt = $("#txtMobile");
                //var url = "/Ajax/AjaxHandle.ashx";

                var params = {
                    CommandName: "UserSecureControlService/SendFindPasswordSMSVCode",
                    Data: { Mobile: usermobileIpt.val(), VerificationCodeType: verificationCodeType, IsFindPwd: true }
                };

                ZYCLib.Web.JsonAjax.NPostData(params.CommandName, params.Data, function (jsondata) {
                    if (jsondata != null) {
                        if (jsondata.ErrorCode === 0) {
                            $("#" + id).addClass("messageing");
                            $("#" + id).html($("#spCountDown").html());

                            var winIntervalID = window.setInterval(function () {
                                RemainSeconds--;
                                if (RemainSeconds > 0) {
                                    $("#Countdown").html(RemainSeconds);
                                } else {
                                    $("#" + id).removeClass("messageing");
                                    $("#" + id).html(text);
                                    RemainSeconds = 60;
                                    $("#Countdown").html(RemainSeconds);
                                    window.clearInterval(winIntervalID);
                                }
                            }, 1000);
                        } else {
                            ShowErrorMessage(jsondata.ErrorMessage);
                        }
                    }
                    
                });
            }

            $("#nextStep").on("click", function() {
                FindPassword();
            });

            $("#lnkGetSMSVCode").on("click", function () {
                SendSVCode("lnkGetSMSVCode", 1);
            });

            $("#lnkGetVoiceVCode").on("click", function () {
                SendSVCode("lnkGetVoiceVCode", 2);
            });

            $("#txtMobile").focus();
        };

        FindPasswordInit();

    })();
})("Page");